
package com.zoo.service

import javax.servlet.ServletConfig
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

import groovy.json.JsonSlurper

class Http extends HttpServlet {

    def sql

    def void init(ServletConfig config) {
	super.init(config)

        sql = new Database()
    }


    def void processRequest(request, response) {

        def json,
        start = legInt(request.getParameter('start'), 0),
        limit = legInt(request.getParameter('limit'), 25),
        sort = legStr(request.getParameter('sort'), ''),
        dir = legStr(request.getParameter('dir'), 'ASC')
        switch(request.getParameter('do')) {

            case 'epy':
                switch(request.getParameter('ex')) {
                    case 'selectUsers'   : json = sql.getEpyUsers(); break
                    case 'selectVendors' : json = sql.getEpyVendors(start, limit); break
                }
                break
            case 'ship':
                switch(request.getParameter('ex')) {
                    case 'selectCust'     : json = sql.selectCustomer(start, limit); break

                    case 'selectUpsZone'  : json = sql.selectUpsZone(start, limit); break
                    case 'updateUpsZone'  : json = sql.updateUpsZone(getJson(request)); break

                    case 'selectUpsRate'  : json = sql.selectUpsRate(start, limit); break
                    case 'updateUpsRate'  : json = sql.updateUpsRate(getJson(request)); break

                    case 'selectUspsZone'  : json = sql.selectUspsZone(start, limit); break
                    case 'updateUspsZone'  : json = sql.updateUspsZone(getJson(request)); break

                    case 'selectUspsRate'  : json = sql.selectUspsRate(start, limit); break
                    case 'updateUspsRate'  : json = sql.updateUspsRate(getJson(request)); break
                }
                break
            case 'google':
                switch(request.getParameter('ex')) {
                    case 'getItems'           : json = sql.getItemsOnGoogle(start, limit, sort, dir); break

                    case 'getVendorsStat'     : json = sql.getVendorsStat(); break
                    case 'getVendorItems'     : json = sql.getVendorItems(start, limit, sort, dir, legInt(request.getParameter('vid'),2)); break // 2 - CVS

                    case 'getTreeCategory'    : json = sql.getTreeCategory(legInt(request.getParameter('node'), 0)); break
                    case 'updateTreeCategory' : json = sql.updateTreeCategory(getJson(request)); break

                    case 'getVendors'         : json = sql.getVendorsCategory(legInt(request.getParameter('cid'),0)); break
                    case 'updateVendors'      : json = sql.updateVendorsCategory(getJson(request)); break
                }
                break

            default: json = '{success:false}'
        }

        response.setContentType("text/html;charset=UTF-8")
        def out = response.getWriter()
        out.print(json)
        out.close()
    }

    def private static legInt(value, deff) {

        def integer = deff
        try {
            integer = (int)Integer.parseInt(value)
        }
        catch(Exception ignore) { }

        return integer as Integer
    }

    def private static legStr(value, deff)  { return value ? value as String : deff  }

    def private static getJson(request)     { return new JsonSlurper().parse(request.getReader())  }

    def void doGet(HttpServletRequest request, HttpServletResponse response) {
        processRequest(request, response)
    }
    def void doPost(HttpServletRequest request, HttpServletResponse response) {
        processRequest(request, response)
    }
}
